﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using Nova_DAL;
using System.Data.Common;
using Nova_Common;
using System.Text;
using System.Data;
using System.IO;
using System.Net.Http;

public partial class Storage_SearchStorage : System.Web.UI.Page
{
    Nova_DAL.DbHelper access = new DbHelper();
    string strSQL = "";
    DbCommand cmd = null;
    string strBaoJin = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }

    protected void bind()
    {
        cmd = access.GetSqlStringCommond(getSQL());
        gv.DataSource = access.ExecuteDataSet(cmd);
        gv.DataBind();
        GroupRows(gv, 1);
        GroupRows(gv, 2);
    }

    public override void VerifyRenderingInServerForm(Control control)
    { }
    protected string getSQL()
    {
        strSQL = @"Select TBP.Pos_ID, TRP.Tray_ID,TBM.Mat_Code,TBM.Mat_Name,TBS.Mat_Num,TBI.TS01,
                        TBM.S01,TBM.TM01,TBM.TM03,TBM.S02,TBM.Unit_Base,TBS.CDT
                        From TBase_Storage TBS LEFT JOIN TRel_TrayPosContainer 
                        TRT ON TBS.ContainerID=TRT.ContainerID LEFT JOIN TBase_TrayPos TBT 
                        ON TBT.Relations_ID = TRT.Relations_ID LEFT JOIN TRelations TRP ON 
                        TRP.Tray_ID = TBT.Tray_ID LEFT JOIN TBase_Position TBP ON TRP.Pos_ID = TBP.Pos_ID 
                        LEFT JOIN TBase_InterFace TBI ON TBI.Sec_Mat_ID =TBS.Sec_Mat_ID LEFT JOIN TBase_Material TBM 
                        ON TBM.Mat_Code = TBI.Mat_Code Where 1=1 ";

        if (txtMat_Code.Text != "")
        {
            strSQL += " and TBM.Mat_Code like '%" + txtMat_Code.Text.ToString().Trim() + "%'";
        }
        if(!string.IsNullOrEmpty(txtMat_Name.Text))
        {
            strSQL += " and TBM.Mat_Name like '%" + txtMat_Name.Text.ToString().Trim() + "%'";
        }
        if (!string.IsNullOrEmpty(txt_TS01.Text))
        {
            strSQL += " and TBI.TS01 like '%" + txt_TS01.Text.Trim() + "%'";
        }
        if (Position1.No_Lane != "0" && Position1.No_Lane != "")
        {
            strSQL += " AND TBP.No_Lane = '" + Position1.No_Lane.ToString() + "'";
        }
        if (Position1.No_Line != "0" && Position1.No_Line != "")
        {
            strSQL += " AND TBP.No_Line = '" + Position1.No_Line.ToString() + "'";
        }

        if (Position1.No_Col != "0" && Position1.No_Col != "")
        {
            strSQL += " AND TBP.No_Col = '" + Position1.No_Col.ToString() + "'";
        }
        if (Position1.No_Layer != "0" && Position1.No_Layer != "")
        {
            strSQL += " AND TBP.No_Layer = '" + Position1.No_Layer.ToString() + "'";
        }
        if (txtTray_ID.Text != "")
        {
            strSQL += " AND TRP.Tray_ID like '%" + txtTray_ID.Text.ToString() + "%'";
        }

        strSQL += " and TBP.Pos_ID != '200000000000' Order By TBT.Tray_ID DESC ";
        return strSQL;
    }

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {

        bind();
    }
    protected void ibtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        Export();
        //Response.Clear();
        //Response.BufferOutput = true;
        ////设定输出的字符集(就是简体中文集)
        //Response.Charset = "GB2312";
        ////导出的文件名为score.xls
        //DateTime dtime = DateTime.Now;

        //Response.AppendHeader("Content-Disposition", "attachment;filename=" + dtime.ToString("库存明细-" + "yyyyMMddHHmmss") + ".xls");
        ////指定的数据是否是ASCII或一种Unicode 变体
        //Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        ////设置导出文件的格式
        //Response.ContentType = "application/ms-excel";
        ////关闭ViewState
        //EnableViewState = false;
        //System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
        //System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
        //System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
        
        //gv.RenderControl(textWriter);
        ////把HTML写回浏览器
        //Response.Write(stringWriter.ToString());
        //Response.End();

    }

    private void Export()
    {
        cmd = access.GetSqlStringCommond(getSQL());
        DataTable dt = access.ExecuteDataTable(cmd);
        string title = string.Format("库存明细-{0}.xls", DateTime.Now.ToString("yyyy-MM-dd"));
        string fileName = HttpUtility.UrlEncode(title, Encoding.UTF8);
        Dictionary<string, string> headCell = new Dictionary<string, string>();
        headCell.Add("Pos_ID", "货位号");
        headCell.Add("Tray_ID", "托盘号");
        headCell.Add("Mat_Code", "物料码");
        headCell.Add("Mat_Name", "物料名称");
        headCell.Add("Mat_Num", "物料数量");
        headCell.Add("TS01", "生产通知单号");
        headCell.Add("S01", "规格");
        headCell.Add("TM01", "材质");
        headCell.Add("TM03", "长度");
        headCell.Add("S02", "备注");
        headCell.Add("Unit_Base", "单位");
        headCell.Add("CDT", "日期");
        MemoryStream ms = new ExcelHelper(title).DataTableToExcel(dt, title, headCell, true);
        string UserAgent = HttpContext.Current.Request.ServerVariables["http_user_agent"].ToLower();
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel;charset=UTF-8";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
        HttpContext.Current.Response.BinaryWrite(ms.ToArray());
        HttpContext.Current.Response.End();
        ms.Close();
        ms = null;
    }
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    Label lbl_TM10 = (Label)e.Row.FindControl("lblTM10");
        //    if (lbl_TM10.Text == "")
        //    {
        //        strBaoJin = "0";
        //    }
        //    else
        //    {
        //        strBaoJin = lbl_TM10.Text.ToString();
        //    }

        //    if (Convert.ToDecimal(e.Row.Cells[5].Text.ToString()) <= Convert.ToDecimal(strBaoJin))
        //    {
        //        e.Row.BackColor = System.Drawing.Color.FromName("#ff0000");
        //    }
        //}  
    }

    public static void GroupRows(GridView GridView1, int cellNum)
    {
        int i = 0, rowSpanNum = 1;
        while (i < GridView1.Rows.Count - 1)
        {
            GridViewRow gvr = GridView1.Rows[i];

            for (++i; i < GridView1.Rows.Count; i++)
            {
                GridViewRow gvrNext = GridView1.Rows[i];
                if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
                {
                    gvrNext.Cells[cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
                    break;
                }

                if (i == GridView1.Rows.Count - 1)
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    }
}